/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Samuel.Macrize.swing.Tree;

import Samuel.Util.PathLoad;
import java.awt.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author samuel
 */
public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {

    private final static String LEVEL_2_NODE_BACKGROUND_UN_SELECT = "l2_unsel.png";
    private final static String LEVEL_2_NODE_BACKGROUND_SELECTED = "l2_se;.png";
    private final static String LEAF_NODE_BACKGROUND_UN_SELECT = "Leaf_unsel.png";
    private final static String LEAF_NODE_BACKGROUND_SELECTED = "Leaf_sel.png";
    private final static String LEVEL_2_NODE_UN_EXPAND_MARK = "Tree_l2_close.png";
    private final static String LEVEL_2_NODE_EXPANDED_MARK = "Tree_l2_open.png";
    private final static int LEVEL_2_NODE_WIDTH = 300;
    private final static int LEVEL_2_NODE_HEIGHT = 30;
    private final static int LEVEL_2_NODE_TEXT_X = 30;
    private final static int LEVEL_2_NODE_TEXT_Y = 20;
    private final static int LEAF_NODE_WIDTH = 300;
    private final static int LEAF_NODE_HEIGHT = 40;
    private final static int LEAF_NODE_TEXT_X = 30;
    private final static int LEAF_NODE_TEXT_Y = 25;
    
    private String ItemTypeImage;
    private Object value;
    private boolean Expanded;

    @Override
    /**
     * 这个是一个定义外观的类 就是继承了 DefaultTreeCellRenderer
     */
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        this.value = value;
        TreeNode[] pathNodes = node.getPath();                                  //取得树的路径

        //判断是否是打开状态 打开返回 true 否则返回 false
        
        TreePath path = tree.getPathForRow(row);
        if (tree.isExpanded(path)) {
            Expanded = true;
        } else {
            Expanded = false;
        }

        // 判断名称，获取不同的图象

        if ("test1".equals(getText())) {
            ItemTypeImage = "k1_cls.png";
        } else {
            ItemTypeImage = " ";
        }


        if (pathNodes.length == 2) {                                            //取得路径长度值，root为1，依此类推
            this.setPreferredSize(new Dimension(LEVEL_2_NODE_WIDTH, LEVEL_2_NODE_HEIGHT));                       //设置了默认窗口的大小值
            //设置了二级（Root被我隐藏了）那一级的宽度高度--windows下有效！
        }

        if (pathNodes.length == 3) {
            this.setPreferredSize(new Dimension(LEAF_NODE_WIDTH, LEAF_NODE_HEIGHT));
        }

        return this;
    }

    /**
     * 这个是绘制一个单元格需要的几个基本元素的封装重构方法
     * @param g2D               这个是graphics2D 的方法
     * @param BackgroundImgName 背景需要贴图的文件名
     * @param x                 起始点的x坐标
     * @param y                 起始点的y坐标
     * @param width             一个单元格的宽度
     * @param height            一个单元格的长度
     * @param textX             文本的起始位置的x坐标
     * @param textY             文本的起始位置的y坐标
     * @param textColor         显示的文本文字颜色
     */
    private void NodeController(Graphics2D g2D, String BackgroundImgName,
            int x, int y, int width, int height,
            int textX, int textY, Color textColor) {
        
        String backgroundImageURL = PathLoad.getImageURL(BackgroundImgName);
        TexturePaint paint = PathLoad.createTexturePaint(backgroundImageURL);
        this.setBounds(x, y, width, height);
        g2D.setPaint(paint);
        g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
        g2D.setColor(textColor);
        g2D.drawString(this.getText(), textX, textY);
    }

    @Override
    public void paint(Graphics grphcs) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        TreeNode[] pathNodes = node.getPath();
        super.paint(grphcs); 
        Graphics2D g2d = (Graphics2D) grphcs;

        //获取 getTreeCellRendererComponent 里面的 value ，获取路径以后，按照路径进行不同的绘图。

        if (pathNodes.length == 2) {
            NodeController(g2d, LEVEL_2_NODE_BACKGROUND_UN_SELECT, 0, 0, 
                    LEVEL_2_NODE_WIDTH, LEVEL_2_NODE_HEIGHT, 
                    LEVEL_2_NODE_TEXT_X, LEVEL_2_NODE_TEXT_Y, Color.WHITE);
            if (Expanded) {
                g2d.drawImage(PathLoad.getImage(LEVEL_2_NODE_EXPANDED_MARK), 10, 10, 10, 10, this);
            } else {
                g2d.drawImage(PathLoad.getImage(LEVEL_2_NODE_UN_EXPAND_MARK), 10, 10, 10, 10, this);
            }
        }

        if (selected && pathNodes.length == 2) {
            NodeController(g2d, LEVEL_2_NODE_BACKGROUND_SELECTED, 0, 0, 
                    LEVEL_2_NODE_WIDTH, LEVEL_2_NODE_HEIGHT, 
                    LEVEL_2_NODE_TEXT_X, LEVEL_2_NODE_TEXT_Y, Color.BLACK);
            if (Expanded) {
                g2d.drawImage(PathLoad.getImage(LEVEL_2_NODE_EXPANDED_MARK), 10, 10, 10, 10, this);
            } else {
                g2d.drawImage(PathLoad.getImage(LEVEL_2_NODE_UN_EXPAND_MARK), 10, 10, 10, 10, this);
            }
        }

        if (pathNodes.length == 3) {
            NodeController(g2d, LEAF_NODE_BACKGROUND_UN_SELECT, 0, 0, 
                    LEAF_NODE_WIDTH, LEAF_NODE_HEIGHT, 
                    LEAF_NODE_TEXT_X, LEAF_NODE_TEXT_Y, Color.BLACK);
            g2d.drawImage(PathLoad.getImage(ItemTypeImage), 0, 5, this);
        }

        if (selected && pathNodes.length == 3) {
            NodeController(g2d, LEAF_NODE_BACKGROUND_SELECTED, 0, 0, 
                    LEAF_NODE_WIDTH, LEAF_NODE_HEIGHT, 
                    LEAF_NODE_TEXT_X, LEAF_NODE_TEXT_Y, Color.WHITE);
            g2d.drawImage(PathLoad.getImage(ItemTypeImage), 0, 5, this);
        }

    }
}
